package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.WorkPlanAssessment;
import com.amsp.oa.oademo.entity.WorkPlanAssessmentDetail;
import com.amsp.oa.oademo.entity.WorkPlanCategory;
import com.amsp.oa.oademo.mapper.WorkPlanAssessmentDetailMapper;
import com.amsp.oa.oademo.mapper.WorkPlanAssessmentMapper;
import com.amsp.oa.oademo.mapper.WorkPlanCategoryMapper;
import com.amsp.oa.oademo.service.WorkPlanAssessmentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class WorkPlanAssessmentServiceImpl extends ServiceImpl<WorkPlanAssessmentMapper, WorkPlanAssessment>
        implements WorkPlanAssessmentService {

    @Autowired
    private WorkPlanAssessmentDetailMapper detailMapper;

    @Autowired
    private WorkPlanCategoryMapper categoryMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WorkPlanAssessment saveWithDetails(WorkPlanAssessment assessment) {
        LocalDateTime now = LocalDateTime.now();

        // 保存主表
        if (assessment.getId() == null) {
            assessment.setCreateTime(now);
        }
        assessment.setUpdateTime(now);
        this.saveOrUpdate(assessment);

        // 保存明细
        if (assessment.getDetails() != null) {
            // 删除原有明细
            detailMapper.delete(
                    new LambdaQueryWrapper<WorkPlanAssessmentDetail>()
                            .eq(WorkPlanAssessmentDetail::getAssessmentId, assessment.getId()));

            // 保存新明细
            assessment.getDetails().forEach(detail -> {
                detail.setAssessmentId(assessment.getId());
                if (detail.getId() == null) {
                    detail.setCreateTime(now);
                }
                detail.setUpdateTime(now);
            });
            assessment.getDetails().forEach(detailMapper::insert);
        }

        return getDetailById(assessment.getId());
    }

    @Override
    public IPage<WorkPlanAssessment> getPage(Integer current, Integer size, String name, String department,
            String position, Integer year, Integer month) {
        Page<WorkPlanAssessment> page = new Page<>(current, size);
        LambdaQueryWrapper<WorkPlanAssessment> wrapper = new LambdaQueryWrapper<>();

        // 添加搜索条件
        if (StringUtils.hasText(name)) {
            wrapper.like(WorkPlanAssessment::getName, name);
        }
        if (StringUtils.hasText(department)) {
            wrapper.like(WorkPlanAssessment::getDepartment, department);
        }
        if (StringUtils.hasText(position)) {
            wrapper.like(WorkPlanAssessment::getPosition, position);
        }
        if (year != null) {
            wrapper.eq(WorkPlanAssessment::getYear, year);
        }
        if (month != null) {
            wrapper.eq(WorkPlanAssessment::getMonth, month);
        }

        // 按创建时间倒序排序
        wrapper.orderByDesc(WorkPlanAssessment::getCreateTime);

        return this.page(page, wrapper);
    }

    @Override
    public WorkPlanAssessment getDetailById(Long id) {
        // 获取主表信息
        WorkPlanAssessment assessment = this.getById(id);
        if (assessment != null) {
            // 获取所有类别
            List<WorkPlanCategory> categories = categoryMapper.selectList(null);
            Map<Long, String> categoryMap = categories.stream()
                    .collect(Collectors.toMap(WorkPlanCategory::getId, WorkPlanCategory::getCategoryName));

            // 获取明细信息
            List<WorkPlanAssessmentDetail> details = detailMapper.selectList(
                    new LambdaQueryWrapper<WorkPlanAssessmentDetail>()
                            .eq(WorkPlanAssessmentDetail::getAssessmentId, id));

            // 设置类别名称
            details.forEach(detail -> detail.setCategoryName(categoryMap.get(detail.getCategoryId())));
            assessment.setDetails(details);
        }
        return assessment;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeWithDetails(Long id) {
        // 删除明细
        detailMapper.delete(
                new LambdaQueryWrapper<WorkPlanAssessmentDetail>()
                        .eq(WorkPlanAssessmentDetail::getAssessmentId, id));

        // 删除主表
        return this.removeById(id);
    }
}